package com.j180.interceptor;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.j180.bean.ResultBean;
import com.j180.bean.User;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class SupplierInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        User user = (User) request.getSession().getAttribute("user");

        if(user != null && ("平台管理员".equals(user.getIdentity()) || "供货商管理员".equals(user.getIdentity()))){
            return true;
        }else{
            ResultBean<String> resultBean = new ResultBean<>();
            ObjectMapper om = new ObjectMapper();
            resultBean.setT("权限不够");
            resultBean.setCode(0);
            resultBean.setMsg("权限不够不能访问");
            String json = om.writeValueAsString(resultBean);
            response.setCharacterEncoding("UTF-8");
            response.setHeader("Content-Type", "text/html;charset=UTF-8");
            response.getWriter().write(json);
            return false;
        }
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
